package com.zwz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwz.bean.CompanyInfo;
import com.zwz.controller.util.Result;
import com.zwz.service.CompanyInfoService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Alysia
 * @createDate 2022/11/10 15:50
 * @description 航空公司信息控制类
 */

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Resource
    private CompanyInfoService companyInfoService;

    /**
     * 增加一条公司信息
     *
     * @param companyInfo 公司信息
     * @return 成功标志
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody CompanyInfo companyInfo) {
        if (companyInfoService.save(companyInfo)) {
            return Result.success("发布成功！");
        } else {
            return Result.error("服务器错误!");
        }
    }

    /**
     * 删除一条公司信息
     *
     * @param companyId 公司id
     * @return 成功标志
     */
    @DeleteMapping("/delete/{companyId}")
    public Result delete(@PathVariable Integer companyId) {
        if (companyInfoService.removeById(companyId)) {
            return Result.success("删除成功！");
        } else {
            return Result.error("服务器错误！");
        }
    }

    /**
     * 修改公司信息
     *
     * @param companyInfo 修改后的公司信息
     * @return 成功标志
     */
    @PutMapping("/update")
    public Result update(@RequestBody CompanyInfo companyInfo) {
        if (companyInfoService.updateById(companyInfo)) {
            return Result.success("修改成功！");
        } else {
            return Result.error("服务器错误！");
        }
    }

    /**
     * 展示某一条数据
     *
     * @param companyId 公司信息id
     * @return 一条公司信息数据
     */
    @GetMapping("/show/{companyId}")
    public Result show(@PathVariable Integer companyId) {
        CompanyInfo companyInfo = companyInfoService.getById(companyId);
        return Result.success(companyInfo);
    }

    /**
     * 分页查询相关
     *
     * @param currentPage 当前页码
     * @param pageSize    每页条数
     * @param companyInfo 查询条件
     * @return 一页公司信息数据
     */
    @GetMapping("/page/{currentPage}/{pageSize}")
    public Result page(@PathVariable int currentPage, @PathVariable int pageSize, CompanyInfo companyInfo) {
        IPage<CompanyInfo> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<CompanyInfo> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(companyInfo.getCompanyAddress()), CompanyInfo::getCompanyAddress, companyInfo.getCompanyAddress());
        lqw.like(Strings.isNotEmpty(companyInfo.getCompanyName()), CompanyInfo::getCompanyName, companyInfo.getCompanyName());

        return Result.success(companyInfoService.page(page, lqw));
    }
}
